//package com.jaws.demo.configuration;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.google.common.collect.Maps;
//import com.jaws.core.common.mybatis.JawsMapperScan;
//import io.shardingjdbc.core.api.ShardingDataSourceFactory;
//import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
//import io.shardingjdbc.core.api.config.TableRuleConfiguration;
//import io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration;
//import io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.mybatis.spring.annotation.MapperScan;
//import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//
//import javax.annotation.Resource;
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.Map;
//import java.util.Properties;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * 账户配置类，代替xml
// *
// * @author liangqm
// * @date 2018/3/23 18:29
// * @since v1.0.0
// */
//@Configuration
//@EnableConfigurationProperties(ShardingConfigProperties.class)
//@JawsMapperScan(value = "${yjs.jaws.mybatis.shardingPackages}", sqlSessionTemplateRef = "shardingSqlSessionTemplate")
//public class ShardingJdbcConfiguration {
//
//	@Resource
//	ShardingConfigProperties shardingConfigProperties;
//
//	@Bean
//	@ConfigurationProperties(prefix = "spring.datasource.shading-jdbc")
//	public DataSource shardingJdbcDataSource() {
//		DruidDataSource datasource = new DruidDataSource();
//		return datasource;
//	}
//
//
////	/**
////	 * account_info 表 分库分表规则设置
////	 * account_info 不做分表
////	 *
////	 * @return
////	 * @author liangqm
////	 * @date 2018-05-02 15:50
////	 * @params
////	 * @since v1.0.0
////	 */
////	public TableRuleConfiguration accountInfoTableRuleConfiguration() {
////		TableRuleConfiguration accountInfoTableRuleConfig = new TableRuleConfiguration();
////		// 指定逻辑表
////		accountInfoTableRuleConfig.setLogicTable("account_info");
////		// 指定数据源节点
////		accountInfoTableRuleConfig.setActualDataNodes("sharding_0.account_info");
////		// 指定分库策略，不分库
////		accountInfoTableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
////		// 指定分表策略: 不分表
////		accountInfoTableRuleConfig.setTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
////		return accountInfoTableRuleConfig;
////	}
//
//
//	/**
//	 * account_trade{0..7} 分库分表规则设置
//	 *
//	 * @return
//	 * @author liangqm
//	 * @date 2018-05-02 15:50
//	 * @params
//	 * @since v1.0.0
//	 */
//	@Bean
//	public TableRuleConfiguration accountTradeTableRuleConfiguration() {
//		TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
//		// 指定逻辑表
//		orderTableRuleConfig.setLogicTable("account_trade");
//		// 指定数据源节点
//		orderTableRuleConfig.setActualDataNodes("sharding_0.account_trade${0..7}");
//		// 指定分库策略，不分库
//		orderTableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
//		// 指定分表策略: 使用 user_id 分表，自定义OrderTableShardingAlgorithm 计算哈希值
//		orderTableRuleConfig.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", "com.jaws.demo.common.sharding.algorithm.AccountTradeTableShardingAlgorithm"));
//		return orderTableRuleConfig;
//	}
//
//	/**
//	 * sharding jdbc 分表规则配置
//	 *
//	 * @return
//	 * @author liangqm
//	 * @date 2018-05-02 15:49:18
//	 * @params
//	 * @since v1.0.0
//	 */
//	@Bean
//	public ShardingRuleConfiguration shardingRuleConfig(TableRuleConfiguration accountTradeTableRuleConfiguration) {
//		ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//		// 绑定分表规则
//		shardingRuleConfig.getTableRuleConfigs().add(accountTradeTableRuleConfiguration);
////		shardingRuleConfig.getTableRuleConfigs().add(accountInfoTableRuleConfiguration);
////		shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
////		shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
//		return shardingRuleConfig;
//	}
//
//	@Bean
//	public DataSource shardingDataSource(DataSource shardingJdbcDataSource, ShardingRuleConfiguration shardingRuleConfig) throws SQLException {
//		// 绑定多数据源
//		Map<String, DataSource> dataSourceMap = Maps.newHashMap();
//		dataSourceMap.put("sharding_0", shardingJdbcDataSource);
//
//		DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), new Properties());
//		return dataSource;
//	}
//
//	/**
//	 * 添加事务
//	 *
//	 * @return
//	 * @author liangqm
//	 * @date 2018-05-02 15:57
//	 * @params
//	 * @since v1.0.0
//	 */
//	@Bean
//	public DataSourceTransactionManager transactitonManager(@Qualifier("shardingDataSource") DataSource dataSource) {
//		return new DataSourceTransactionManager(dataSource);
//	}
//
//
//	@Resource
//	MybatisProperties mybatisProperties;
//
//	@Bean(name = "shardingSqlSessionFactory")
//	public SqlSessionFactory shardingSqlSessiFonactory(@Qualifier("shardingDataSource") DataSource dataSource) throws Exception {
//		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//		bean.setDataSource(dataSource);
//		// 设置 mapper xml 扫描路径
//		bean.setMapperLocations(mybatisProperties.resolveMapperLocations());
//		return bean.getObject();
//	}
//
//
//	@Bean(name = "shardingSqlSessionTemplate")
//	@Primary
//	public SqlSessionTemplate shardingSqlSessionTemplate(@Qualifier("shardingSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
//		return new SqlSessionTemplate(sqlSessionFactory);
//	}
//
//}
